package com.mbrs.schedule;

import com.mbrs.msg.MsgSender;
import com.mbrs.service.LoginService;
import erp.repository.TakeEntityException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Configuration
@EnableScheduling
public class ScheduleController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private MsgSender msgSender;

    @Scheduled(fixedRate = 5000)
    public void checkSessionOvertimeAndRemove() {
        List<String> sessionIds = loginService.queryAllSessionIds();
        if (sessionIds != null) {
            for (String sid : sessionIds) {
                try {
                    loginService.checkAndRemoveOvertimeSession(sid,
                            System.currentTimeMillis());
                } catch (TakeEntityException e) {
                    continue;
                }
                msgSender.sendProcess(true);
            }
        }
    }

}
